const sameObj = { b: 2 }
/**数组去重 - 测试用例 */
export const removeTest_arr = [5, 5, '666', '666', NaN, NaN, null, null, undefined, undefined, { a: 1 }, { a: 1 }, sameObj, sameObj]
/**数组去重 - set方法 - 可去除NaN*/
export const removeSame_set = <T>(arr: T[]): T[] => {
    return [...new Set(arr)]
}
/**数组去重 - 哈希表 - 可去除NaN */
export const removeSame_hash = <T>(arr: T[]): T[] => {
    const hash = new Set<T>()
    const res: T[] = [] //为了不改变原数组
    for (let i = 0; i < arr.length; i++) {
        const element = arr[i];
        if (!hash.has(element)) {
            hash.add(element)
            res.push(element)
        }
    }
    return res
}
/**数组去重 - 双指针 - 只适用于数字数组(等可排序数组)，因为需要排序 */
export const removeSame_TwoPoint = (arr: number[]) => {
    arr.sort((a, b) => a - b);
    const result = [arr[0]];
    let j = 0;

    for (let i = 1; i < arr.length; i++) {
        if (arr[i] !== arr[j]) {
            result.push(arr[i]);
            j = i;
        }
    }
    return result;
}

// console.log('原', removeTest_arr);
// console.log('set', removeSame_set(removeTest_arr));
// console.log('哈希表', removeSame_hash(removeTest_arr));